package com.sxm.yiwei.activitis;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.text.Editable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sxm.yiwei.MainActivity;
import com.sxm.yiwei.R;
import com.sxm.yiwei.utils.InternetAddressUtis;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import static android.R.attr.id;

public class MineAccountOutMoney extends BaseActivity implements View.OnClickListener{
    private String TAG ="out";
    private ImageView back;
    private TextView outmoney;
    private EditText inmoney;
    private TextView accountno;
    private Button outsure;
    private SharedPreferences sharedPreferences;
    private String appId;
    private String userId;
    private String token;
    private String amount;
    private String caution;
    private String service;

    private RelativeLayout chooseaccount;
    private String id;
    private String payeeAccount;
    private String realName;
    private ImageView backmain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine_account_out_money);
        sharedPreferences = getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        appId = sharedPreferences.getString("appid", "0");
        userId = sharedPreferences.getString("userId", "1");
        token = sharedPreferences.getString("token", "2");
        initOutView();
        getAcountData();
        getOutAccountData();
    }
        // 获取默认账户接口
    private void getOutAccountData() {
        RequestParams params = new RequestParams(InternetAddressUtis.Url+InternetAddressUtis.GETDEFULTEACCOUNT);
        params.addHeader("Content-Type","application/json");
        params.addHeader("token", token);
        params.addHeader("appId", appId);
        params.addHeader("userId", userId);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.d(TAG, "onSuccess: 默认账户--"+result);
                JSONObject object = JSON.parseObject(result);
                String data = object.getString("data");
                JSONObject obj = JSON.parseObject(data);
                id = obj.getString("id");
                payeeAccount = obj.getString("payeeAccount");
                realName = obj.getString("realName");
                accountno.setText(payeeAccount);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.d(TAG, "onError: 默认账户--"+ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }

    private void getAcountData() {
        RequestParams params = new RequestParams(InternetAddressUtis.Url+"/account/getAccountInfo");
        params.addHeader("Content-Type","application/json");
        params.addHeader("token", token);
        params.addHeader("appId", appId);
        params.addHeader("userId", userId);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                com.alibaba.fastjson.JSONObject obj = JSON.parseObject(result);
                String data = obj.getString("data");
                com.alibaba.fastjson.JSONObject obj1 = JSON.parseObject(data);
                amount = obj1.getString("amount");
                caution = obj1.getString("caution");
                service = obj1.getString("service");
                outmoney.setText("余额: "+amount+"元");

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
    private void initOutView() {
        backmain = (ImageView) findViewById(R.id.accountout_backmain);
        back = (ImageView) findViewById(R.id.mine_accountout_back);
        outmoney = (TextView) findViewById(R.id.mine_out_money);
        inmoney = (EditText) findViewById(R.id.mine_inoutnumber);
        accountno = (TextView) findViewById(R.id.mine_out_accountno);
        outsure = (Button) findViewById(R.id.mine_outsure);
        chooseaccount = (RelativeLayout) findViewById(R.id.out_chooseaccount);
        back.setOnClickListener(this);
        outsure.setOnClickListener(this);
        chooseaccount.setOnClickListener(this);
        backmain.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.accountout_backmain:
                startActivity(new Intent(MineAccountOutMoney.this, MainActivity.class));
                AppManager.getAppManager().finishAllActivity();

                break;
            case R.id.mine_outsure:
                final String money = String.valueOf(inmoney.getText());
                if (money.equals("")){
                    Toast.makeText(this, "请填写提现金额", Toast.LENGTH_SHORT).show();
                }else {
                    Double lastmm = Double.valueOf(amount);
                    Double outmm = Double.valueOf(money);
                    if (outmm>lastmm){
                        Toast.makeText(this, "余额不足", Toast.LENGTH_SHORT).show();
                    }else {
                        if (id == null) {
                            Toast.makeText(this, "请选择提现账户", Toast.LENGTH_SHORT).show();
                        } else {
                            AlertDialog.Builder builder = new AlertDialog.Builder(MineAccountOutMoney.this, R.style.NoBackGroundDialog);
                            View view = LayoutInflater.from(MineAccountOutMoney.this).inflate(R.layout.cancel_servicedialog, null);
                            builder.setView(view);
                            Button surebt = (Button) view.findViewById(R.id.cancel_surebt);
                            Button cancelbt = (Button) view.findViewById(R.id.cancel_cancelbt);
                            TextView t2 = (TextView) view.findViewById(R.id.cancel_t2);
                            t2.setText("是否提现到: " + realName + " " + payeeAccount + "？");
                            surebt.setText("提现");
                            final AlertDialog dialog = builder.create();
                            surebt.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    OutMoney(money, id);
                                    dialog.dismiss();
                                }
                            });
                            cancelbt.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    dialog.dismiss();
                                }
                            });
                            dialog.show();
                        }
                    }
                }

                break;
            case R.id.mine_accountout_back:
                finish();
                break;
            case R.id.out_chooseaccount:
                Intent in = new Intent(MineAccountOutMoney.this,ChooseAliAcount.class);
                startActivityForResult(in,500);
                break;
        }
    }
        // 提现
    private void OutMoney(String money, String id) {
        RequestParams params = new RequestParams(InternetAddressUtis.Url+InternetAddressUtis.APPLYOUT);
        params.addHeader("Content-Type","application/json");
        params.addHeader("token", token);
        params.addHeader("appId", appId);
        params.addHeader("userId", userId);
        JSONObject object = new JSONObject();
        object.put("cashAccountId",id);
        object.put("amount",money);
        params.setBodyContent(object.toJSONString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                JSONObject object1 = JSON.parseObject(result);
                String status = object1.getString("status");
                if (status.equals("1")){
                    Toast.makeText(MineAccountOutMoney.this, "亲，您的提现申请已提交。我们每周一申请，周二对账，周三打款", Toast.LENGTH_SHORT).show();
                }else if (status.equals("0")){
                    String failure = object1.getString("failure");
                    if (failure.equals("1054")){
                        Toast.makeText(MineAccountOutMoney.this, "提现申请时间只能为周一", Toast.LENGTH_SHORT).show();
                    }else if (failure.equals("1055")){
                        Toast.makeText(MineAccountOutMoney.this, "申请审核中,请不要重复提交", Toast.LENGTH_SHORT).show();

                    }else {}
                }else {}
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data!=null){
            if (requestCode==500){
                Bundle extras = data.getExtras();
                realName = extras.getString("realName");
                payeeAccount = extras.getString("payeeAccount");
                id = extras.getString("id");
                accountno.setText(payeeAccount);
            }else {}
        }else {}


    }
}
